package com.winit.page.repository;

import com.datastax.driver.core.querybuilder.QueryBuilder;
import com.datastax.driver.core.querybuilder.Select;
import com.datastax.driver.core.querybuilder.Update;
import com.winit.feature.model.Feature;
import com.winit.feature.model.FeatureReq;
import com.winit.feature.repository.FeatureRepository;
import com.winit.page.model.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.cassandra.core.CassandraOperations;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * Created by wangjian on 2017/9/28.
 */
@Component
public class PageOperations {

    private static Logger logger = LoggerFactory.getLogger(PageOperations.class);

    private final String TABLE = "matrix_page";

//    private AtomicInteger num = new AtomicInteger(0);
//
//    private AtomicLong time = new AtomicLong(0);

    @Autowired
    private CassandraOperations cassandraOperations;




    public int updatePropertiesNotNullById(Page page) {
        Update update = QueryBuilder.update(TABLE);

        if (page.getMenuId() != null) {
            update.with(QueryBuilder.set("ref_menu_id", page.getMenuId()));
        }
        if (page.getTitle() != null) {
            update.with(QueryBuilder.set("title", page.getTitle()));
        }

        if (page.getFileName() != null) {
            update.with(QueryBuilder.set("file_name", page.getFileName()));
        }

        if (page.getMenuPath() != null) {
            update.with(QueryBuilder.set("menu_path", page.getMenuPath()));
        }
        if (page.getModels() != null) {
            update.with(QueryBuilder.set("models", page.getModels()));
        }
        if (page.getChildren() != null) {
            update.with(QueryBuilder.set("children", page.getChildren()));
        }
        update.where(QueryBuilder.eq("id", page.getId()));
        cassandraOperations.execute(update);
        return 1;
    }

//    public List<Feature> findByProperNotNull(Feature feature) {
//        Select select = QueryBuilder.select().from(TABLE);
//        if (feature.getId() != null) {
//            select.where(QueryBuilder.eq("id", feature.getId()));
//        }
//        if (feature.getSys() != null) {
//            select.where(QueryBuilder.eq("sys", feature.getSys()));
//        }
//        if (feature.getPath() != null) {
//            select.where(QueryBuilder.eq("path", feature.getPath()));
//        }
//        if (feature.getStatus() != null) {
//            select.where(QueryBuilder.eq("status", feature.getStatus()));
//        }
//        if (null != feature.getAttrConfigId()) {
//            select.where(QueryBuilder.eq("attr_config_id", feature.getAttrConfigId()));
//        }
//        if (null != feature.getReqId()) {
//            select.where(QueryBuilder.eq("req_id", feature.getReqId()));
//        }
//        /**
//         * 2017-11-29，将feature_name由config移向feature中
//         */
//        if (null != feature.getFeatureName()) {
//            select.where(QueryBuilder.eq("feature_name", feature.getFeatureName()));
//        }
//        select.allowFiltering();
//
//        return cassandraOperations.select(select, Feature.class);
//    }
//
//
//    public List<FeatureReq> findFeatureReqByFeatureId(String featureId){
//        Select select = QueryBuilder.select().from("matrix_feature_req");
//        select.where(QueryBuilder.eq("feature_id", featureId));
//        select.allowFiltering();
//        return cassandraOperations.select(select, FeatureReq.class);
//    }

}
